﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>JS Console - Example</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<div id="js-console">
	</div>
	<script src="scripts/js-console.js">
	</script>
	<script>

		function getWords(text) {
			var words = text.split(/[\s\.,-?!)(]/);
			for (var i = 0; i < words.length; i++) {
				if (words[i] == "") {
					words.splice(i, 1);
				}
			}
			return words;
		}

		function countWords(words) {
			var wordsCount = {};
			var word = {};
			for (var i in words) {
				word = words[i].toLowerCase();
				if (wordsCount[word]) {
					wordsCount[word]++;
				}
				else {
					wordsCount[word] = 1;
				}
			}

			return wordsCount;
		}

		var text = "When I was born my parents named me Pesho Ivanov, and still my name is Pesho Ivanov and now I am 102 years old. Two years ago my first name was Pesho, and my last name was Ivanov. ";


		var words = getWords(text);

		var wordsCount = countWords(words);

		for (var word in wordsCount) {
			jsConsole.writeLine(word + " -> " + wordsCount[word]);
		}
	</script>
</body>
</html>
